// 这个文件专门用来存放关于首页的所有接口
const Router = require('koa-router');
const goods = new Router();
const db = require('../utils/db');
const bodyparser = require('koa-bodyparser')
goods.use(bodyparser());
// 写对应的接口
// 所有商品
goods.get('/', async (ctx) => {
    let mydata = await new Promise((resolve, reject) => {
        // 访问数据库，拿到banner这个数据给前端
        return db.query(`select * from goods`, (err, data) => {
            if (err) throw err;
            console.log(data);
            data.map(val => {
                val.goodsimg = 'http://localhost:5050' + val.goodsimg;
                val.picture = 'http://localhost:5050' + val.picture;
            })
            resolve(data);
        });
    })
    ctx.body = mydata;
})
// 进入商品详情
goods.post('/goodsDetail', async (ctx) => {
    const goodsid = ctx.request.body.goodsid;
    console.log(goodsid);
    let mydata = await new Promise((resolve, reject) => {
        // 访问数据库，拿到banner这个数据给前端
        return db.query(`select * from goods where goodsid='${goodsid}'`, (err, data) => {
            if (err) throw err;
            console.log(data);
            data.map(val => {
                val.goodsimg = 'http://localhost:5050' + val.goodsimg;
                var picture = JSON.parse(val.picture);
                for(var i=0;i<picture.length;i++){
                    picture[i]='http://localhost:5050' + picture[i];
                }
                val.picture=picture;
            })
            resolve(data);
        });
    })
    ctx.body = mydata;
})
// 商品评论
goods.post('/goodsComment', async (ctx, next) => {
    const goodsid = ctx.request.body.goodsid;
    console.log(goodsid);
    let mydata = await new Promise((resolve, reject) => {
        // 访问数据库，拿到banner这个数据给前端
        return db.query(`select * from commentMenu where goodsid='${goodsid}'`, (err, data) => {
            if (err) throw err;
            // console.log(data);
            resolve(data);
        });
    })
    ctx.body = mydata;
})
// 我的评论
goods.post('/myComment', async (ctx, next) => {
    const openid = ctx.request.body.openid;
    console.log(openid);
    console.log('商品评价信息');
    const commentSql = `select * from commentMenu where openid='${openid}'`
    let mydata = await new Promise((resolve, reject) => {
        // 访问数据库，拿到banner这个数据给前端
        return db.query(commentSql, (err, data) => {
            if (err) throw err;
            console.log(data);
            resolve(data);
        });
    })
    ctx.body = mydata;
})
// 我的售出
goods.post('/mySaleOut', async (ctx, next) => {
    const openid = ctx.request.body.openid;
    console.log(openid);
    console.log('商品评价信息');
    const saleOutSql = `select * from goods where openid='${openid}'`
    let mydata = await new Promise((resolve, reject) => {
        // 访问数据库，拿到banner这个数据给前端
        return db.query(saleOutSql, (err, data) => {
            if (err) throw err;
            data.map(val => {
                val.goodsimg = 'http://localhost:5050' + val.goodsimg;
                val.picture = 'http://localhost:5050' + val.picture;
            })
            console.log(data);
            resolve(data);
        });
    })
    ctx.body = mydata;
})

// 进行商品发布
goods.post('/public', async (ctx, next) => {
    var i = 0;
    console.log("接收到post发布商品请求");
    // await next();
    const data = ctx.request.body;
    console.log(data);
    var goodsImages = JSON.stringify(data.goodsImages);
    let publicSql = `insert into goods (openid,goodsname,intro,picture,sortid,price,secprice,username,goodsnum,goodsimg) values ` +
        `('${data.openid}','${data.goodsname}','${data.goodsintro}','${goodsImages}','${data.typeId}',` +
        `'${data.price}','${data.goodsprice}','${data.username}','${data.goodsnumber}','${data.goodsCoverImage}')`;
    let mydata = await new Promise((resolve, reject) => {
        // 访问数据库，拿到banner这个数据给前端
        return db.query(publicSql, (err, data) => {
            if (err) throw err;
            console.log(data);
            resolve(data);
        });
    })
    ctx.body = mydata;
    console.log('成功插入数据！')
})

// 进行商品的删除操作
goods.post('/deleteGood', async (ctx, next) => {
    const goodsid = ctx.request.body.goodsid;
    console.log(goodsid);
    console.log('商品评价信息');
    const deleteSql = `delete from goods where goodsid='${goodsid}'`
    let mydata = await new Promise((resolve, reject) => {
        // 访问数据库，拿到banner这个数据给前端
        return db.query(deleteSql, (err, data) => {
            if (err) throw err;
            console.log(data);
            resolve(data);
        });
    })
    console.log('删除成功');
    ctx.body = mydata;
})

module.exports = goods;